import 'dart:convert';

import 'package:dsbridge_flutter/dsbridge_flutter.dart';
import 'package:ecy_app_flutter/EcyRouter.dart';
import 'package:ecy_app_flutter/constant/color_constant.dart';
import 'package:ecy_app_flutter/ecycommon/utils/EcyLogger.dart';
import 'package:ecy_app_flutter/ecycommon/utils/EcyQQUtil.dart';
import 'package:ecy_app_flutter/ecycommon/utils/EcyUserUtil.dart';
import 'package:ecy_app_flutter/ecycommon/utils/EcyWxUtil.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluwx/fluwx.dart';

import '../constant/EcyEventConsnt.dart';
import '../constant/constant.dart';
import '../main.dart';
import 'EcyEventBus.dart';

class JsApi extends JavaScriptNamespaceInterface {
  final BuildContext context;
  JsApi(this.context);
  @override
  void register() {
    registerFunction(testSyn);
    registerFunction(callNativeApi);
    registerFunction(getBaseInfo);
    registerFunction(onAddNewReceiveGoodsAddress);
    registerFunction(onGoGoodsCart);
    registerFunction(onBuy);
    registerFunction(onShareGoods);
    registerFunction(onNaviBack);
    registerFunction(onShowLoading);
    registerFunction(onStopLoading);
    registerFunction(onNaviToLogin);
    registerFunction(onLogout);
    registerFunction(onShareToWx);
    registerFunction(getStatusHeight);
    registerFunction(onGoodsCartChanged);
  }

  /// for synchronous invocation
  String testSyn(dynamic msg) {
    EcyLogger.log("testSyn");
    return "$msg［syn call2］";
  }

  String callNativeApi(dynamic msg) {
    EcyLogger.log("callNativeApi");
    return "$msg［syn call2］";
  }

  String getBaseInfo(dynamic msg) {
    String user = json.encode(EcyUserUtil.userModel);
    EcyLogger.log("getBaseInfo--->" + user);
    return user;
  }

  String getStatusHeight(dynamic msg) {
    EcyLogger.log("getStatusHeight--->");
    return EcyPageUtil.getStatusBarHeight(context).toString();
  }

  void onAddNewReceiveGoodsAddress(dynamic msg) {
    EcyLogger.log("onAddNewReceiveGoodsAddress");
    EasyLoading.dismiss();
    EcyRouter.push(context, EcyRouter.addressPage, {});
  }

  void onGoGoodsCart(dynamic msg) {
    EcyLogger.log("onGoGoodsCart");
    EasyLoading.dismiss();
    EcyRouter.push(context, EcyRouter.goodsCartHomePage, null);
  }

  void onBuy(dynamic msg) {
    EcyLogger.log("onBuy-->" + msg);
    EasyLoading.dismiss();
    EcyRouter.push(context, EcyRouter.orderConfirm, msg);
  }

  void onShareGoods(dynamic msg) {
    EcyLogger.log("onShareGoods" + msg.toString());
  }

  void onShowLoading(dynamic msg) {
    EcyLogger.log("onShowLoading" + msg.toString());
    EasyLoading.show(status: '加载中...');
  }

  void onStopLoading(dynamic msg) {
    EcyLogger.log("onStopLoading" + msg.toString());
    EasyLoading.dismiss();
  }

  void onNaviBack(dynamic msg) {
    EcyLogger.log("onNaviBack" + msg.toString());
    EcyRouter.pop(context, "");
  }

  void onNaviToLogin(dynamic msg) {
    EcyLogger.log("onNaviToLogin" + msg.toString());
    // EcyRouter.pushWithNeedLogin(context, EcyRouter.ipSelectPage,
    //     {EcyRouter.kTargetUrl: EcyRouter.ipSelectPage});
    EcyRouter.pushWithNeedLogin(context, msg ?? EcyRouter.passwordLoginPage,
        {EcyRouter.kTargetUrl: msg});
  }

  void onLogout(dynamic msg) {
    EcyLogger.log("onLogout" + msg.toString());
    EcyUserUtil.clearUserInfo();
  }

  void onShareToWx(dynamic msg) {
    EcyLogger.log("onShareToWx" + msg.toString());
    Map<String, dynamic> shareOb = json.decode(msg);
    //TODO 后续加分享平台选择 默认走微信
    String p = shareOb["sharePlatform"];
    if (p.isNotEmpty) {
      if (p == "0") {
        EcyWxUtil.shareText(msg);
      } else {
        EcyQQUtil.shareText(msg);
      }
    } else {
      EcyWxUtil.shareText(msg);
    }
  }

  void onGoodsCartChanged(dynamic msg) {
    EcyLogger.log("onGoodsCartChanged");
    EcyEventBus().emit(EcyEventConst.kCartChanged);
  }

}
